package com.hava.contentsale.web.controller;

import com.hava.contentsale.meta.AccountVO;
import com.hava.contentsale.meta.UserVO;
import com.hava.contentsale.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by yanfa on 2016/11/20.
 * R5 账务
 * R5.1 在展示界面点击“账务”按钮，进入账务界面 - 在所有需要提交user信息得部分正确提交买者，则header正确显示
 * R5.2 在账务界面以列表方式列出所有已购买的内容
 * R5.3 列表的最后计算出总的金额 - 该内容有前端处理展示
 *
 * 账务的每一项详细定义见AccountVO
 */
@Controller
public class AccountController {

    @Autowired
    AccountService accountServiceImpl;

    @RequestMapping(value = "/account")
    public ModelAndView account_view(HttpSession httpSession)
    {
        System.out.println("Class AccountController Method account_view");
        //session
        System.out.println("[session.id]:" + httpSession.getId());
        UserVO userVO = (UserVO) httpSession.getAttribute("user");


        //当用户没有登录时，跳转回登录界面
        if(userVO == null)
            return new ModelAndView("redirect:login");
            //如果用户类型为买家，则跳转到发布界面
        else if(userVO.getUsertype() == 0)
        {
            ModelAndView modelAndView = new ModelAndView("account");
            //当用户不为空时，返回用户信息
            if(userVO != null)
                modelAndView.addObject("user", userVO);

            List<AccountVO> accountVOs = this.accountServiceImpl.findAll(userVO);
            //由于freemarker模板使用的是buyList
            modelAndView.addObject("buyList", accountVOs);

            return modelAndView;
        }
        else
            return new ModelAndView("redirect:login");

    }
}
